<?php
require_once 'db.php';

// 获取产品总数
function getTotalProducts() {
    global $conn;
    $sql = "SELECT COUNT(*) as total FROM products";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    return $row['total'];
}

// 获取应用领域总数
function getTotalApplications() {
    global $conn;
    $sql = "SELECT COUNT(DISTINCT category) as total FROM applications";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    return $row['total'];
}

// 获取客户咨询总数
function getTotalContacts() {
    global $conn;
    $sql = "SELECT COUNT(*) as total FROM contacts";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    return $row['total'];
}

// 获取新闻总数
function getTotalNews() {
    global $conn;
    $sql = "SELECT COUNT(*) as total FROM news";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    return $row['total'];
}

// 获取最近客户咨询
function getRecentContacts($limit = 5) {
    global $conn;
    $sql = "SELECT * FROM contacts ORDER BY created_at DESC LIMIT $limit";
    $result = mysqli_query($conn, $sql);
    $contacts = array();
    while($row = mysqli_fetch_assoc($result)) {
        $contacts[] = $row;
    }
    return $contacts;
}

// 获取最近新闻
function getRecentNews($limit = 5) {
    global $conn;
    $sql = "SELECT * FROM news ORDER BY created_at DESC LIMIT $limit";
    $result = mysqli_query($conn, $sql);
    $news = array();
    while($row = mysqli_fetch_assoc($result)) {
        $news[] = $row;
    }
    return $news;
}

// 获取所有产品
function getAllProducts() {
    global $conn;
    $sql = "SELECT * FROM products ORDER BY id DESC";
    $result = mysqli_query($conn, $sql);
    $products = array();
    while($row = mysqli_fetch_assoc($result)) {
        $products[] = $row;
    }
    return $products;
}

// 获取单个产品
function getProduct($id) {
    global $conn;
    $id = mysqli_real_escape_string($conn, $id);
    $sql = "SELECT * FROM products WHERE id = $id";
    $result = mysqli_query($conn, $sql);
    return mysqli_fetch_assoc($result);
}

// 添加产品
function addProduct($name, $category, $description, $image) {
    global $conn;
    $name = mysqli_real_escape_string($conn, $name);
    $category = mysqli_real_escape_string($conn, $category);
    $description = mysqli_real_escape_string($conn, $description);
    $image = mysqli_real_escape_string($conn, $image);

    $sql = "INSERT INTO products (name, category, description, image, created_at) VALUES ('$name', '$category', '$description', '$image', NOW())";
    return mysqli_query($conn, $sql);
}

// 更新产品
function updateProduct($id, $name, $category, $description, $image) {
    global $conn;
    $id = mysqli_real_escape_string($conn, $id);
    $name = mysqli_real_escape_string($conn, $name);
    $category = mysqli_real_escape_string($conn, $category);
    $description = mysqli_real_escape_string($conn, $description);
    $image = mysqli_real_escape_string($conn, $image);

    $sql = "UPDATE products SET name='$name', category='$category', description='$description', image='$image' WHERE id=$id";
    return mysqli_query($conn, $sql);
}

// 删除产品
function deleteProduct($id) {
    global $conn;
    $id = mysqli_real_escape_string($conn, $id);
    $sql = "DELETE FROM products WHERE id=$id";
    return mysqli_query($conn, $sql);
}

// 获取所有应用领域
function getAllApplications() {
    global $conn;
    $sql = "SELECT DISTINCT category FROM applications";
    $result = mysqli_query($conn, $sql);
    $applications = array();
    while($row = mysqli_fetch_assoc($result)) {
        $applications[] = $row;
    }
    return $applications;
}

// 获取特定应用领域的产品
function getApplicationProducts($category) {
    global $conn;
    $category = mysqli_real_escape_string($conn, $category);
    $sql = "SELECT * FROM applications WHERE category='$category' ORDER BY id DESC";
    $result = mysqli_query($conn, $sql);
    $products = array();
    while($row = mysqli_fetch_assoc($result)) {
        $products[] = $row;
    }
    return $products;
}

// 添加应用领域产品
function addApplicationProduct($category, $name, $description, $image, $properties) {
    global $conn;
    $category = mysqli_real_escape_string($conn, $category);
    $name = mysqli_real_escape_string($conn, $name);
    $description = mysqli_real_escape_string($conn, $description);
    $image = mysqli_real_escape_string($conn, $image);
    $properties = mysqli_real_escape_string($conn, $properties);

    $sql = "INSERT INTO applications (category, name, description, image, properties, created_at) VALUES ('$category', '$name', '$description', '$image', '$properties', NOW())";
    return mysqli_query($conn, $sql);
}

// 更新应用领域产品
function updateApplicationProduct($id, $category, $name, $description, $image, $properties) {
    global $conn;
    $id = mysqli_real_escape_string($conn, $id);
    $category = mysqli_real_escape_string($conn, $category);
    $name = mysqli_real_escape_string($conn, $name);
    $description = mysqli_real_escape_string($conn, $description);
    $image = mysqli_real_escape_string($conn, $image);
    $properties = mysqli_real_escape_string($conn, $properties);

    $sql = "UPDATE applications SET category='$category', name='$name', description='$description', image='$image', properties='$properties' WHERE id=$id";
    return mysqli_query($conn, $sql);
}

// 删除应用领域产品
function deleteApplicationProduct($id) {
    global $conn;
    $id = mysqli_real_escape_string($conn, $id);
    $sql = "DELETE FROM applications WHERE id=$id";
    return mysqli_query($conn, $sql);
}

// 获取所有新闻
function getAllNews() {
    global $conn;
    $sql = "SELECT * FROM news ORDER BY created_at DESC";
    $result = mysqli_query($conn, $sql);
    $news = array();
    while($row = mysqli_fetch_assoc($result)) {
        $news[] = $row;
    }
    return $news;
}

// 获取单个新闻
function getNews($id) {
    global $conn;
    $id = mysqli_real_escape_string($conn, $id);
    $sql = "SELECT * FROM news WHERE id = $id";
    $result = mysqli_query($conn, $sql);
    return mysqli_fetch_assoc($result);
}

// 添加新闻
function addNews($title, $content, $image) {
    global $conn;
    $title = mysqli_real_escape_string($conn, $title);
    $content = mysqli_real_escape_string($conn, $content);
    $image = mysqli_real_escape_string($conn, $image);

    $sql = "INSERT INTO news (title, content, image, created_at) VALUES ('$title', '$content', '$image', NOW())";
    return mysqli_query($conn, $sql);
}

// 更新新闻
function updateNews($id, $title, $content, $image) {
    global $conn;
    $id = mysqli_real_escape_string($conn, $id);
    $title = mysqli_real_escape_string($conn, $title);
    $content = mysqli_real_escape_string($conn, $content);
    $image = mysqli_real_escape_string($conn, $image);

    $sql = "UPDATE news SET title='$title', content='$content', image='$image' WHERE id=$id";
    return mysqli_query($conn, $sql);
}

// 删除新闻
function deleteNews($id) {
    global $conn;
    $id = mysqli_real_escape_string($conn, $id);
    $sql = "DELETE FROM news WHERE id=$id";
    return mysqli_query($conn, $sql);
}

// 获取所有客户咨询
function getAllContacts() {
    global $conn;
    $sql = "SELECT * FROM contacts ORDER BY created_at DESC";
    $result = mysqli_query($conn, $sql);
    $contacts = array();
    while($row = mysqli_fetch_assoc($result)) {
        $contacts[] = $row;
    }
    return $contacts;
}

// 删除客户咨询
function deleteContact($id) {
    global $conn;
    $id = mysqli_real_escape_string($conn, $id);
    $sql = "DELETE FROM contacts WHERE id=$id";
    return mysqli_query($conn, $sql);
}

// 上传图片
function uploadImage($file, $target_dir) {
    // 检查文件是否是真实的图片
    $check = getimagesize($file["tmp_name"]);
    if($check === false) {
        return array("success" => false, "message" => "文件不是图片");
    }

    // 检查文件大小
    if ($file["size"] > 5000000) {
        return array("success" => false, "message" => "文件太大");
    }

    // 允许的文件格式
    $imageFileType = strtolower(pathinfo($file["name"], PATHINFO_EXTENSION));
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
        return array("success" => false, "message" => "只允许JPG, JPEG, PNG & GIF文件");
    }

    // 生成唯一文件名
    $target_file = $target_dir . uniqid() . '.' . $imageFileType;

    // 上传文件
    if (move_uploaded_file($file["tmp_name"], $target_file)) {
        return array("success" => true, "path" => $target_file);
    } else {
        return array("success" => false, "message" => "上传失败");
    }
}
?>